package cn.tx.dao.impl;

import org.apache.commons.lang.StringUtils;

import cn.tx.dao.ProductDao;
import cn.tx.model.Product;
import cn.tx.query.ProductQuery;

public class ProductDaoImpl extends BaseDaoImpl<Product, ProductQuery> implements ProductDao {

	@Override
	public String createHql(ProductQuery q) {
		String hql = "from Product e where 1=1 ";
		String condition = this.createConditionHql(q);
		hql = hql + condition + "order by e.productId desc";
		return hql;
	}

	@Override
	public String createHqlCount(ProductQuery q) {
		String hql = "select count(e) from Product e where 1=1 ";
		//StringUtils.isNotBlank判断当前的字符串是不是空串和null
		String condition = this.createConditionHql(q);
		hql = hql + condition;
		return hql;
	}

	@Override
	public String createConditionHql(ProductQuery q) {
		String hql = "";
		if(q.getSupplierId() != null){
			hql = hql + "and e.productType.supplier.supplierId = :supplierId ";
		}
		if(StringUtils.isNotBlank(q.getName())){
			hql = hql + "and e.name like :name ";
		}
		if(StringUtils.isNotBlank(q.getProducer())){
			hql = hql + "and e.producer like :producer ";
		}
		if(StringUtils.isNotBlank(q.getUnit())){
			hql = hql + "and e.unit like :unit ";
		}
		if(q.getInPriceMin() != null){
			hql = hql + "and e.inPrice > :inPriceMin ";
		}
		if(q.getInPriceMax() != null){
			hql = hql + "and e.inPrice < :inPriceMax ";
		}
		if(q.getOutPriceMin() != null){
			hql = hql + "and e.outPrice > :outPriceMin ";
		}
		if(q.getOutPriceMax() != null){
			hql = hql + "and e.outPrice < :outPriceMax ";
		}
		return hql;
	}



}
